﻿using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.Globalization;
using System.Windows.Forms;

namespace IE_Functions
{
    public class EntryCreator
    {
        static RegistryKey adresse;
        //static string[] adressen = { "wurzelimperium.de", "molehillempire.com", "molehillempire.nl", "molehillempire.fr", "bg.molehillempire.com", "sadowajaimperija.ru", "zieloneimperium.pl", "molehillempire.es", "bahcivanlardiyari.com", "zeleneimperium.cz", "molehillempire.se", "kertbirodalom.hu", "molehillempire.com.pt", "molehillempire.ro", "kipourosendrasi.gr", "molehillempire.it", "molehillempire.dk", "molehillempire.com.br" };
        static RegistryKey currentUser;
        static RegistryKey rServer;
        //static int[] server = { 46, 4, 2, 3, 2, 5, 21, 3, 5, 4, 2, 5, 2, 2, 2, 1, 1, 1 };
        static RegistryKey zonemapDomains;
        
        public static void CreateEntries(string[] adressen, int[] server)
        {
            // Es folgt meine eigene "Implementierung" der Resources-Klasse...
            LanguageStrings langStrings = new LanguageStrings(CultureInfo.CurrentCulture.TwoLetterISOLanguageName);

            // Den IE können wir nicht gebrauchen... ;-)
            while (Process.GetProcessesByName("iexplore").Length > 0)
            {
                if (MessageBox.Show(langStrings.IEIsRunning, langStrings.Warning, MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning) == DialogResult.Cancel)
                {
                    return;
                }
            }

            try
            {
                // Wir müssen leider mit der Registrierung arbeiten...
                currentUser = Registry.CurrentUser;
                zonemapDomains = currentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ZoneMap\\Domains", RegistryKeyPermissionCheck.ReadWriteSubTree);

                for (int i = 0; i < adressen.Length; i++)
                {
                    for (int j = 1; j <= server[i]; j++)
                    {
                        adresse = zonemapDomains.CreateSubKey(adressen[i], RegistryKeyPermissionCheck.ReadWriteSubTree);
                        rServer = adresse.CreateSubKey("s" + j.ToString(), RegistryKeyPermissionCheck.ReadWriteSubTree);
                        rServer.SetValue("http", 2, RegistryValueKind.DWord);
                        rServer.Close();
                        adresse.Close();
                    }
                }

                zonemapDomains.Close();
                currentUser.Close();
                MessageBox.Show(langStrings.Done, langStrings.Information, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                // Ich mache es mir einfach...
                MessageBox.Show(ex.ToString(), langStrings.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                if (rServer != null) rServer.Close();
                if (adresse != null) adresse.Close();
                if (zonemapDomains != null) zonemapDomains.Close();
                if (currentUser != null) currentUser.Close();
            }
        }
    }
}
